//
//  DemoTabbarController.m
//  RongIMDemo
//
//  Created by 崔逢举 on 2020/2/10.
//  Copyright © 2020 崔逢举. All rights reserved.
//

#import "DemoTabbarController.h"
#import "HomeViewController.h"
@interface DemoTabbarController ()

@property NSUInteger previousIndex;

@end

@implementation DemoTabbarController

+ (DemoTabbarController *)sharedInstance {
    static DemoTabbarController *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[[self class] alloc] init];
    });
    return instance;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setControllers];
    [self setTabBarItems];
    self.delegate = self;
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(changeSelectedIndex:)
                                                  name:@"ChangeTabBarIndex"
                                                object:nil];
}

//设置一级控制器
- (void)setControllers {
     HomeViewController *homeVC = [[HomeViewController alloc]init];
     self.viewControllers = @[homeVC];
}

//配置一级控制器
- (void)setTabBarItems {
    [self.viewControllers
           enumerateObjectsUsingBlock:^(__kindof UIViewController *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
               if ([obj isKindOfClass:[HomeViewController class]]) {
                   obj.tabBarItem.title = LocalizedString(@"conversation");
                   obj.tabBarItem.image =
                       [[UIImage imageNamed:@"icon_chat"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                   obj.tabBarItem.selectedImage =
                       [[UIImage imageNamed:@"icon_chat_hover"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
               } else {
                   NSLog(@"Unknown TabBarController");
               }
           }];
}

- (void)tabBarController:(UITabBarController *)tabBarController
    didSelectViewController:(UIViewController *)viewController {
    NSUInteger index = tabBarController.selectedIndex;
    [DemoTabbarController sharedInstance].selectedTabBarIndex = index;
    switch (index) {
    case 0: {
        if (self.previousIndex == index) {
            //判断如果有未读数存在，发出定位到未读数会话的通知
            if ([[RCIMClient sharedRCIMClient] getTotalUnreadCount] > 0) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"GotoNextConversation" object:nil];
            }
            self.previousIndex = index;
        }
        self.previousIndex = index;
    } break;

    case 1:
        self.previousIndex = index;
        break;

    default:
        break;
    }
}

- (void)changeSelectedIndex:(NSNotification *)notify {
    NSInteger index = [notify.object integerValue];
    self.selectedIndex = index;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
